package viewcontrollers.util;

import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.model.SelectItem;
import pojos.Currencies;
import pojos.Customers;
import pojos.Employees;
import pojos.Expenses;
import pojos.JobsTitles;
import pojos.Projects;
import pojos.SubcontarctorResources;

public class JsfUtil {

    public static SelectItem[] getSelectItems(List<?> entities, boolean selectOne) {
        int size = selectOne ? entities.size() + 1 : entities.size();
       
      
       Object oneObject=entities.get(0);
       if(oneObject instanceof Employees|oneObject instanceof SubcontarctorResources)
       {
           size=size-1;
       }
        SelectItem[] items = new SelectItem[size];
        
         int i = 0;
        if (selectOne) {
            items[0] = new SelectItem("", "---");
            i++;
        }
        
        for (Object x : entities) {
            
            if(x instanceof Customers)
            {
                Customers customer=(Customers)x;
                items[i++] = new SelectItem(customer,customer.getName());
            }
           
          else   if(x instanceof Currencies)
            {
                Currencies currency=(Currencies)x;
                items[i++] = new SelectItem(currency,currency.getCurrencyCode());
            }
           else   if(x instanceof Customers)
            {
                Customers customer=(Customers)x;
                items[i++] = new SelectItem(customer,customer.getName());
            }
            else   if(x instanceof Employees)
            {
                Employees emp=(Employees)x;
                if(emp.getEmpId()!=1)
                items[i++] = new SelectItem(emp,emp.getFirstName());
            }
               
              else  if(x instanceof Expenses)
            {
                Expenses exp=(Expenses)x;
                items[i++] = new SelectItem(exp,exp.getExpenseType());
            }
                
               else  if(x instanceof JobsTitles)
            {
                JobsTitles job=(JobsTitles)x;
                items[i++] = new SelectItem(job,job.getJobTitle());
            }
            
                else   if(x instanceof Projects)
            {
                Projects pro=(Projects)x;
                items[i++] = new SelectItem(pro,pro.getProjectName());
            }
            
                 else   if(x instanceof SubcontarctorResources)
            {
                SubcontarctorResources sub=(SubcontarctorResources)x;
              if(sub.getSubResId()!=1)
                items[i++] = new SelectItem(sub,sub.getSubResName());
            }
            
             else{
                   items[i++] = new SelectItem(x,x.toString());   
             }
            
        }
        return items;
    }

//    public static SelectItem[] getJobsTitlesItems(List<JobsTitles> entities, boolean selectOne) {
//        int size = selectOne ? entities.size() + 1 : entities.size();
//        SelectItem[] items = new SelectItem[size];
//        int i = 0;
//        if (selectOne) {
//            items[0] = new SelectItem("", "---");
//            i++;
//        }
//        for (JobsTitles x : entities) {
//            items[i++] = new SelectItem(x.getJobTitle(), x.getJobTitle().toString());
//        }
//        return items;
//    }
    
    
    public static void addErrorMessage(Exception ex, String defaultMsg) {
        String msg = ex.getLocalizedMessage();
        if (msg != null && msg.length() > 0) {
            addErrorMessage(msg);
        } else {
            addErrorMessage(defaultMsg);
        }
    }

    public static void addErrorMessages(List<String> messages) {
        for (String message : messages) {
            addErrorMessage(message);
        }
    }

    public static void addErrorMessage(String msg) {
        FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
        FacesContext.getCurrentInstance().addMessage(null, facesMsg);
    }

    public static void addSuccessMessage(String msg) {
        FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg);
        FacesContext.getCurrentInstance().addMessage("successInfo", facesMsg);
    }

    public static String getRequestParameter(String key) {
        return FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(key);
    }

    public static Object getObjectFromRequestParameter(String requestParameterName, Converter converter, UIComponent component) {
        String theId = JsfUtil.getRequestParameter(requestParameterName);
        return converter.getAsObject(FacesContext.getCurrentInstance(), component, theId);
    }
}
